package kr.co.icebird.temp;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.*;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XMLEx02 extends DefaultHandler {

	SAXParser parser;
	Map mp;

	public XMLEx02() {
		mp = new HashMap();
	}

	public Map getMp() {
		return mp;
	}

	public void reader() throws SAXException, IOException,
			ParserConfigurationException {
		SAXParserFactory reader = SAXParserFactory.newInstance();
		reader.newSAXParser().parse(
				new File("src\\main\\java\\kr\\co\\icebird\\temp\\sample.xml"),
				this);
	}

	public void startDocument() {
		System.out.println("Start document");
	}

	public void endDocument() {
		System.out.println("End document");
	}

	public void startPrefixMapping(String prefix, String uri) {
		System.out.println("Mapping starts for prefix " + prefix
				+ " mapped to URI " + uri);
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {

		if ("".equals(uri)) {
			System.out.println("Start element : " + qName);
		} else {
			System.out.println("Start element : {" + uri + "}" + localName);
		}
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if ("".equals(uri)) {
			System.out.println("End element : " + qName);
		} else {
			System.out.println("End element : {" + uri + "}" + localName);
		}
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {

		String s = new String(ch, start, length);
		System.out.println("characters: " + s);
/*
		System.out.println("Characters :  \"");
		for (int i = start; i < start + length; i++) {
			switch (ch[i]) {
			case '\\':
				System.out.print("\\\\");
				break;
			case '"':
				System.out.print("\\\"");
				break;
			case '\n':
				System.out.print("\\n");
				break;
			case '\r':
				System.out.print("\\r");
				break;
			case '\t':
				System.out.print("\\t");
				break;
			default:
				System.out.print(ch[i]);
				break;
			}
		}
		System.out.print("\"\n");
		
		*/
	}

	public static void main(String[] args) throws SAXException, IOException,
			ParserConfigurationException {
		XMLEx02 ex02 = new XMLEx02();
		ex02.reader();
		System.out.println("================================================");
		System.out.println(ex02.getMp());
	}

}
